/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bibliotekosis;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import org.javalite.activejdbc.Base;

/**
 *
 * @author Vytautas
 */
public class DB
{
    public static void init() 
    {   
        Properties props = new Properties();
       try {
          props.load(new FileInputStream("options.cfg.properties"));
       } catch (IOException e) {
          e.getStackTrace();
       }
        Base.open("com.mysql.jdbc.Driver", "jdbc:mysql://" + props.getProperty("DB_ADDR").toString() + "/" + props.getProperty("DB_NAME").toString(), props.getProperty("DB_USER").toString(), props.getProperty("DB_PASS").toString());
    }
    
    public static void close()
    {
        Base.close();
    }
    
    public static boolean findWorker(String login, String pass)
    {
        DB.init();
        Worker u = Worker.findFirst("login = ? AND password = SHA1(?)", login, pass);
        DB.close();
        
        return (u != null);
    }
    
    public static List searchMaterial(String searchTerm)
    {
        DB.init();
        List<Material> material = Material.where("name LIKE '%?%'", searchTerm)
                .limit(15)
                .orderBy("receival_date DESC");
        DB.close();
        
        return material;
    }
    
}
